package com.atguigu.aclservice.controller;


import com.atguigu.aclservice.entity.Role;
import com.atguigu.aclservice.entity.vo.AssginRoleVo;
import com.atguigu.aclservice.service.RoleService;
import com.atguigu.commonutils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author fxy
 * @since 2022-10-27
 */
@Api(tags = "角色管理接口")
@Slf4j
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("/acl/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

//    @Log(title = "获取用户的角色数据",businessType = BusinessType.ASSGIN)
    @ApiOperation("获取用户的角色数据")
    @GetMapping("toAssign/{userId}")
    public R toAssign(@PathVariable String userId) {
        Map<String,Object> roleMap = roleService.getRolesByUserId(userId);
        return R.ok().data(roleMap);
    }

//    @Log(title = "用户分配角色",businessType = BusinessType.ASSGIN)
    @ApiOperation("用户分配角色")
    @PostMapping("doAssign")
    public R doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        roleService.doAssign(assginRoleVo);
        return R.ok();
    }

    //7 批量删除
    // 多个id值 [1,2,3]
    // json数组格式 --- java的list集合
//    @Log(title = "批量删除",businessType = BusinessType.DELETE)
    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public R batchRemove(@RequestBody List<Long> ids) {
        roleService.removeByIds(ids);
        return R.ok();
    }

    //6 修改-最终修改
//    @Log(title = "最终修改",businessType = BusinessType.UPDATE)
    @ApiOperation("最终修改")
    @PostMapping("update")
    public R updateRole(@RequestBody Role sysRole) {
        boolean isSuccess = roleService.updateById(sysRole);
        if(isSuccess) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //5 修改-根据id查询
//    @Log(title = "根据id查询",businessType = BusinessType.FIND)
    @ApiOperation("根据id查询")
    @PostMapping("findRoleById/{id}")
    public R findRoleById(@PathVariable Long id) {
        Role sysRole = roleService.getById(id);
        return R.ok().data("sysRole",sysRole);
    }

    //4 添加
    // @RequestBody 不能使用get提交方式
    // 传递json格式数据，把json格式数据封装到对象里面 {...}
//    @Log(title = "添加角色",businessType = BusinessType.INSERT)
    @ApiOperation("添加角色")
    @PostMapping("save")
    public R saveRole(@RequestBody Role sysRole) {
        boolean isSuccess = roleService.save(sysRole);
        if(isSuccess) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //3 条件分页查询
    // page当前页  limit每页记录数
    @ApiOperation("条件分页查询")
    @PostMapping("/findPageQueryRole/{page}/{size}")
    public R findPageQueryRole(@PathVariable Long page, @PathVariable Long size, @RequestParam(required = false) String roleName){

        Page<Role> roomPage=new Page<>(page,size);
        QueryWrapper<Role> wrapper=new QueryWrapper<>();


        if (roleName!=null){
            wrapper.like("role_name",roleName);
        }


        roleService.page(roomPage,wrapper);
        List<Role> roleList = roomPage.getRecords();
        long total = roomPage.getTotal();
        return R.ok().data("roleList",roleList).data("total",total);
    }

    //2 逻辑删除接口
//    @Log(title = "逻辑删除接口",businessType = BusinessType.DELETE)
    @ApiOperation("逻辑删除接口")
    @DeleteMapping("remove/{id}")
    public R removeRole(@PathVariable Long id) {
        //调用方法删除
        boolean isSuccess = roleService.removeById(id);
        if(isSuccess) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //1 查询所有记录
//    @Log(title = "逻辑删除接口",businessType = BusinessType.FIND)
    @ApiOperation("查询所有记录")
    @GetMapping("findAll")
    public R findAllRole() {

        //调用service
        List<Role> list = roleService.list(null);
        return R.ok().data("list",list);
    }
}

